package com.example.springbootaop.word;

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;

public class TestWord02 {
	public static void main(String[] args) {
		//加载Word文档
		Document doc = new Document();
		doc.loadFromFile("Images.docx");

		//获取文档中的第一个节
		Section section = doc.getSections().get(0);

		//遍历该节中的所有段落
		for (Object object : (Iterable) section.getParagraphs()) {
			Paragraph para = (Paragraph) object;
			//遍历每个段落中的子元素
			for (Object o : (Iterable) para.getChildObjects()) {
				DocumentObject obj = (DocumentObject) o;
				//使用新图片替换文档中的现有图片
				if (obj instanceof DocPicture) {
					DocPicture pic = (DocPicture) obj;
					pic.loadImage("Hydrangeas.jpg");
				}
			}
		}

		//保存结果文档
		doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013);
	}
}
